/*
 *	自定义的密码强度 
 * 
 */
var styles=new Array();
styles[0] = {backgroundColor:"#EBEBEB",borderLeft:"solid 1px #FFFFFF",borderRight:"solid 1px #BEBEBE",borderBottom:"solid 1px #BEBEBE"};	
styles[1] = {backgroundColor:"#FF4545",borderLeft:"solid 1px #FFFFFF",borderRight:"solid 1px #BB2B2B",borderBottom:"solid 1px #BB2B2B"};
styles[2] = {backgroundColor:"#FFD35E",borderLeft:"solid 1px #FFFFFF",borderRight:"solid 1px #E9AE10",borderBottom:"solid 1px #E9AE10"};
styles[3] = {backgroundColor:"#95EB81",borderLeft:"solid 1px #FFFFFF",borderRight:"solid 1px #3BBC1B",borderBottom:"solid 1px #3BBC1B"};

var divName="pwd_div";
function switchPic(id,isOver){ //转换图片位置
	if(isOver){
		jQ('#'+id).css({"background-position": "0px 0px" });
	}else{
		jQ('#'+id).css({"background-position": "0px -24px" });
	}
}
function copyToStyle(id){ //转换text的状态
	for(var i=0;i<3;i++){
		if(i == id-1){
			document.getElementById(divName+"_label_"+i).style.display = "inline";
		}else{
			document.getElementById(divName+"_label_"+i).style.display = "none";
		}
	}
	for(var i=0;i<id;i++){
		copyToObject(styles[id],document.getElementById(divName+"_td_"+i).style);			
	}
	for(;i<3;i++){
		copyToObject(styles[0],document.getElementById(divName+"_td_"+i).style);
	}
}

function copyToObject(o1,o2){ //复制
	for(var i in o1){
		o2[i] = o1[i];
	}
}

function updatePwd(s){ 
	if(s.length < 5){
		copyToStyle(0);
		return;
	}
	var ls = -1;
	if (s.match(/[a-z]/ig)){
		ls++;
	}
	if (s.match(/[0-9]/ig)){
		ls++;
	}
 	if (s.match(/(.[^a-z0-9])/ig)){
		ls++;
	}
	if (s.length < 6 && ls > 0){
		ls--;
	}
	 switch(ls) { 
		 case 0:
			 copyToStyle(1);
			 break;
		 case 1:
			 copyToStyle(2);
			 break;
		 case 2:
			 copyToStyle(3);
			 break;
		 default:
			 copyToStyle(0);
	 }
}
//	--自定义密码强度结束--



var rightIcon = '<img src="image/regimg/right.png" width="14" height="14" border="0" align="absmiddle">&nbsp;';
var icon = '<img src="image/regimg/wrong.png" width="14" height="14" border="0" align="absmiddle">&nbsp;';
var ns = ["usr","pwd","repwd","eml","vcode"];
function checkUsr(s){		
	var ma = ["用户名（3-16位）！","用户名由中文、数字、英文、下划线、中杠线组成！"];
	if(!limitLen(s,3,16)){
		showInfo("usr",ma[0]);
		return false;
	}
	if(!hasAccountChar(s)&&!hasChineseChar(s)){
		showInfo("usr",ma[1]);
		return false;
	}
	showInfo("usr");
	return true;
}
function checkPwd(s){
	var ma = ["密码（5-16位）！","密码不能包含中文或全角符号！","两次输入的密码不一致！"];
	updatePwd(s);
	if(!limitLen(s,5,16)){
		showInfo("pwd",ma[0]);
		return false;
	}
	if(hasChineseChar(s)){
		showInfo("pwd",ma[1]);
		return false;
	}
	if(jQ("#repwdInput").val()==""||trim(jQ("#repwdInput").val())==""){
		showInfo("pwd");
		return;
	}
	if(limitLen(jQ("#repwdInput").val(),5,16)){
		if(trim(jQ("#repwdInput").val()) == trim(s)){
			showInfo("pwd");
			showInfo("repwd");
			return true;
		}else{
			showInfo("pwd",ma[2]);
			showInfo("repwd",ma[2]);
			return false;
		}
	}
	showInfo("pwd");
	return true;
}
function checkPwd2(s){
	var ma = ["确认密码（5-16位）！","密码不能包含中文或全角符号！","两次输入的密码不一致！"];
	if(!limitLen(s,5,16)){
		showInfo("repwd",ma[0]);
		return false;
	}
	if(hasChineseChar(s)){
		showInfo("repwd",ma[1]);
		return false;
	}
	if(limitLen(jQ("#pwdInput").val(),5,16)){
		if(trim(jQ("#pwdInput").val()) == trim(s)){
			showInfo("pwd");
			showInfo("repwd");
			return true;
		}else{
			showInfo("repwd",ma[2]);
			return false;
		}
	}
	showInfo("repwd");
	return true;
}
function checkEml(s){
	var ma = ["请输入常用邮件！","邮件格式不正确！"];
	if(s.length < 5){
		showInfo("eml",ma[0]);
		return false;
	}
	if(!isEmail(s)){
		showInfo("eml",ma[1]);
		return false;
	}
	showInfo("eml");
	return true;
}

function showInfo(n,s){
	jQ('#returnMSG').empty();
	var fdo = jQ("#"+n+"FrameDiv");
	var ido = jQ("#"+n+"InfoDiv");
	if(typeof s == 'undefined'||s==null){
		if(n=='vcode'){
			//.removeClass();
			//fdo.addClass("FrameDivPass");
			ido.empty();
			ido.append(rightIcon+"填写正确！");
		}else{
			//fdo.removeClass();
			//fdo.addClass("FrameDivPass");
			ido.empty();
			ido.append(rightIcon+"填写正确！");
		}
	}else{
		ido.empty();
		ido.append(icon + s);
	}
}

function updateCss(obj,flag){
	if(flag){
		jQ(obj).addClass("NFfocused");
		jQ(obj).next().addClass("NFfocused");
		jQ(obj).prev().addClass("NFfocused");
	}
}
//======================================================;
//ajax
function jqAjax(s){
	if(!checkUsr(s))
		return;
	var uname =jQ("#usrInput").val();//oCtl.value;
	if(trim(uname)=="")
		return;
	var uri = "umreg.action?ischeckName=1&name="+uname;
	//var o = jQ('checkDiv');
	loadCheck();
	jQuery(function(jQ){
		var url=uri;
		jQ.ajax({
			type: "POST",
			url:url,
			success:function(data){
				getCheckHTML(trim(data));
			}
		});
	});
}


function loadCheck(){
	//if(trim($F('usrInput')).length == 0) return;
	//$("checkBtn").disabled = true;
	var o = jQ("#usrInfoDiv");
	o.empty();
	o.append(getLoadInfo());
	//loadAjaxData("reg.asp",{usr:$F('usrInput')},successCheck,errorCheck);
}
function errorCheck(){
	//$("checkBtn").disabled = false;
	var o = jQ("#usrInfoDiv");
	o.empty();
	o.append(getErrorInfo());
}
function getCheckHTML(s){
	//jQ('#usrFrameDiv').removeClass();
	if(s=='true'){
		//jQ('#usrFrameDiv').addClass("FrameDivPass");
		jQ('#usrInfoDiv').empty();
		jQ('#usrInfoDiv').append(rightIcon+"恭喜您，用户名可以注册！");
	}else{
		//jQ('#usrFrameDiv').addClass("FrameDivWarn");
		jQ('#usrInfoDiv').empty();
		jQ('#usrInfoDiv').append(icon+"对不起，该用户名已被注册！");
	}
}
//======================================================;
function getLoadInfo(){
	return '<img src="image/regimg/loading.gif" width="16" height="16" border="0" align="absmiddle">正在加载数据...';
}
function getErrorInfo(){
	return '<img src="image/regimg/warning.gif" width="14" height="14" border="0" align="absmiddle">数据加载失败!';
}
//======================================================;
function initPage(){
	for(var i=0;i<ns.length;i++){
		jQ("#"+ns[i]+"Input").val("") ;
		jQ("#"+ns[i]+"InfoDiv").empty() ;
		//jQ("#"+ns[i]+"FrameDiv").removeClass();
		//jQ("#"+ns[i]+"FrameDiv").addClass("FrameDivNormal");
	}
}

function resetPWD(){
	jQ("#pwdInput").val("") ;
	jQ("#pwdInfoDiv").empty() ;
	//jQ("#pwdFrameDiv").removeClass();
	//jQ("#pwdFrameDiv").addClass("FrameDivNormal");
	jQ("#repwdInput").val("") ;
	jQ("#repwdInfoDiv").empty() ;
	//jQ("#repwdFrameDiv").removeClass();
	//jQ("#repwdFrameDiv").addClass("FrameDivNormal");
}

function regVaildate(){
	if(jQ('#statmentCB').attr("checked")==false){
		jQ('#returnMSG').empty();
		jQ('#returnMSG').append('您必须同意注册协议才能注册。');
		return false;
	}
	//checkVcode(document.getElementById('vcodeInput').value)
	//alert(tem);
	if(checkUsr(jQ('#usrInput').val()) && checkPwd(jQ('#pwdInput').val())
			 && checkPwd2(jQ('#repwdInput').val()) && isGoodYzm){
		return true;
	}else
		return false;
	//验证码是否通过
	if(checkYzm(jQ('#vcodeInput').val()))
		return true;
	else
		return false
}
var isGoodYzm=false;
function checkYzm(s){
	var ma = ["请输入验证码!","请正确输入验证码！"];
	if(trim(s)==""){
		showInfo("vcode",ma[0]);
		return false;
	}
	var url="checkYzm.action";
	showInfo("vcode",null);
	//isGoodYzm=true;
	jQ.ajax({
		type: "POST",
		url:url,
		data:{vcodeInput : jQ('#vcodeInput').val()
			},
		success:function(data){
			if(data=='success'){
				showInfo("vcode",null);
				isGoodYzm=true;
				return true;
			}else{
				showInfo("vcode",ma[1]);
				return false;
			}
		}
	});
}

function changeImg(){     
     var imgSrc = jQ("#imgcode");    
 var src = imgSrc.attr("src");     
 imgSrc.attr("src",chgUrl(src));     
 }     
 //时间戳     
 //为了使每次生成图片不一致，即不让浏览器读缓存，所以需要加上时间戳     
 function chgUrl(url){     
	 var timestamp = (new Date()).valueOf();     
     urlurl = url + "?time=" + timestamp;     
     return urlurl;     
 }
function onSubmit(){
	checkIsLogin();
}

function continueSubmit(){
	if(regVaildate()){
		jQ('#loading').attr('style', 'display:block;');
		jQ('#formpage').attr('style', 'display:none;');
		setTimeout(function(){
			}
			, 1000);
		var url = "umreg.action";
		jQ.ajax({
			type: "POST",
			url:url,
			data:{usrInput : jQ('#usrInput').val(),
				pwdInput:jQ('#pwdInput').val(),
				vcodeInput:jQ('#vcodeInput').val(),
				emlInput:jQ('#emlInput').val()
				},
			success:function(data){
						jQ('#loading').attr('style', 'display:none;');
                        jQ('#formpage').attr('style', 'display:block;');
                        var dataObj=eval("("+data+")");//转换为json对象
						var array=data.split("=");
						if(dataObj['registerflag']){
							jQ("div.register_info").empty();
							jQ("div.register_info").append(dataObj['ucsylogin']);
							jQ('#returnMSG').empty();
							jQ('#returnMSG').append(rightIcon+'<font color="#31a9c4">&nbsp;'+dataObj['message']+',页面会在2秒内自动关闭</font>');
							setTimeout(function(){
								loginAfterReg();
								//onCancel();
								//关闭层
								//jQ("#btn_nyroModalClose").click();
								}
							, 2000);
						}else{
							resetPWD();
							jQ('#returnMSG').empty();
							jQ('#returnMSG').append(icon+'<font color="#31a9c4">&nbsp;'+dataObj['message']+'</font>')
							}
				}
			});
		}
 }

function loginAfterReg(){
	var url="login.action";
	jQ.ajax({
		type: "POST",
		url:url,
		data:{auto:true,op:'login'},
		success:function(data){
			
			if(data!=null){
				var dataObj=eval("("+data+")");//转换为json对象
				if(dataObj['loginflag']){
					jQ("#mainA").click();//去首页
					/*jQ('div.logo-box div.logo-box-in div.login_error_div').hide();
					jQ("div.logo-box div.logo-box-in").hide();
					jQ("div.logo-box div.logo-box-out").show();
					setUserInfo('username',dataObj);
					setUserInfo('usericon',dataObj);
					setUserInfo('level',dataObj);
					setUserInfo('newmsg',dataObj);
					setUserInfo('helloinfo',dataObj);*/
				}else{
					/*jQ('div.logo-box div.logo-box-in div.login_error_div').show();
					jQ('div.logo-box div.logo-box-in div.login_error_div span.login_error_info').empty();
					jQ('div.logo-box div.logo-box-in div.login_error_div span.login_error_info').append(dataObj['message']);
					jQ("div.logo-box div.logo-box-in").show();
					jQ("div.logo-box div.logo-box-out").hide();*/
				}
			}
			
			
			
			
			
			
//			if(data.indexOf('username')>=0){//登陆成功
//				ucsynlogin();
//				//登录成功后
//				setIsLogin(true);
//				//jQ("#toggle_search").css("background","url(image/siteImage/loginout_tab_icon.jpg) 0 0 no-repeat");
//				jQ("#toggle_search").css({"background-position":"0 0"});
//				var message=data.split('='); //刪除分号
//				jQ("#userMsg").empty();
//				jQ("#userMsg").append(message[3]);
//				jQ("#navigation ul li.msg a").show();
//				jQ("#toggle_head").hide();
//				jQ("#changepwd").show();
//			}
		}
	});
}
 function onCancel(){
	 if(locktoggle_head)
			return;
		istoggle_headclicked=false;
		jQ('#headerContainer>.header').fadeOut();
		jQ('#headerContainer').slideToggle('slow',function(){
			locktoggle_head=false;
		});
		locktoggle_head=true;
 }
 function onReset(){
	 initPage();
	 copyToStyle(0);
 }
 

var istoggle_headclicked=false;
var locktoggle_head=false;
function checkIsLogin(){
	jQ.ajax({
		type: 'POST',
		url:'checkLogin.action',
		success:function(data){
			if(data==1){
				jQ("#ALERT p").empty().html('您已经在登录状态，请退出后再进行注册。');
				jQ("#ALERT").dialog('open');
				setTimeout(function(){
					jQ("#mainA").click();
					}
				, 2000);
			}else if(data==0){
				continueSubmit();
			}
		}
	});
}

